package com.bw.demo_widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * @Author : ZhouZiYao
 * @Time : On 2023/11/24 15:28
 * @Description :
 */
public class MyView extends View {

    private Paint paint;//画笔
    private int color = Color.RED;//默认颜色
    private float radius = 30;//默认半径 30

    /**
     * 初始化画笔
     */
    private void initPaint() {
        paint = new Paint();
        paint.setColor(color);//设置颜色
        paint.setAntiAlias(true);//抗锯齿
        paint.setStrokeWidth(10);//设置宽度
        paint.setStyle(Paint.Style.FILL);//设置填充模式:FILL实心
        paint.setDither(true);//防抖动
        paint.setStrokeCap(Paint.Cap.ROUND);//设置线段末端为圆形
        paint.setStrokeJoin(Paint.Join.ROUND);//设置线段连接处为圆形
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //绘制椭圆
        RectF rectF = new RectF(0, 0, 300, 200);
        canvas.drawOval(rectF, paint);
    }

    /**
     * 测量View
     * 1.测量规格 32位 = 测量规格 30位 + 测量大小 2位
     * 2.三种测量模式
     *  (1).EXACTLY精准测量300dp/200dp match_parent
     *  (2).AT_MOST最大测量:wrap_contene
     *  (3).UNSPECIFIED不顶顶测量：不常用 ListView, recyclerview
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //宽度：通过测量规格获得测量模式
        int mode_w = MeasureSpec.getMode(widthMeasureSpec);
        //宽度：通过测量规格获得测量大小
        int size_w = MeasureSpec.getSize(widthMeasureSpec);
        if (mode_w == MeasureSpec.AT_MOST){
            size_w = 300;
        }

        //高度：通过测量规格获得测量模式
        int mode_h = MeasureSpec.getMode(widthMeasureSpec);
        //高度：通过测量规格获得测量大小
        int size_h = MeasureSpec.getSize(widthMeasureSpec);
        if (mode_h == MeasureSpec.AT_MOST){
            size_h = 200;
        }

        //设置view的宽高
        setMeasuredDimension(size_w, size_h);
    }

    //构造方法
    public MyView(Context context) {
        super(context);
        initPaint();
    }

    //xml布局
    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        //获取自定义属性
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyView);
        color = typedArray.getColor(R.styleable.MyView_my_color, Color.RED);
        radius = typedArray.getFloat(R.styleable.MyView_my_radius,30);
        //一定记得回收资源!!!
        typedArray.recycle();
        initPaint();
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initPaint();
    }
}
